/*********************************************************
*  @file    cm_uart_demo.c
*  @brief   ML302 OpenCPU uart APIs example
*  Copyright (c) 2019 China Mobile IOT.
*  All rights reserved.
*  created by XieGangLiang 2019/10/17
********************************************************/
#include "cm_main.h"
#include "atol_all_head.h"

/*----------------------------------------------------------------------------------------------------
# 函数功能 数据定义
# 参数介绍
----------------------------------------------------------------------------------------------------*/
// 定义串口选择
#define OPENCPU_UART0 CM_UART_0

// 定义任务handle
osThreadId ATOL_Uart0_TaskHandle;


unsigned int uart0_data_arrived = 0;
unsigned char uart0_buf[1024];
int uart0_buf_len = 0;

// 定义互斥量
osMutexDef( uart0_cmdMutex );
osMutexId uart0_cmd_mutex;


unsigned char * uart0_cmd_buf[20]; //最多接受20个参数
int uart0_uart0_cmd_len;


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart0的接收中断，接收到数据时更新标志位
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static void atol_oc_uart0_cb(void *param,unsigned int evt)
{
   uart0_data_arrived =1;
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart0的接收方法，接收数据都在其中处理
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static atol_firmware_uart0_recv_task(void *param)
{
    unsigned char buf[64] = {0};
    unsigned char * p;
    int len ;
	
    while(true)
    {
        if(uart0_data_arrived == 1)
        {             
            uart0_buf_len = cm_uart_receive(OPENCPU_UART0,uart0_buf,1024);
            
            while(uart0_buf_len < 1024)
            {
                len = cm_uart_receive(OPENCPU_UART0,uart0_buf+uart0_buf_len,1024-uart0_buf_len);
                
                if(len == 0)
                    break;
                uart0_buf_len += len;
            } 
            
			// 输出打印
			cm_printf("atol_firmware_uart0_recv_task %s \r\n", uart0_buf);
			
			// 存入到消息队列中
			unsigned int status;
			status = osMessagePut(g_message_uart0_receive_id, buf, 100);
			
			// demo中的原来程序
            // if(osMutexWait( uart0_cmd_mutex, 0 ) != 0)
            // {
                // cm_printf("busy\n");
                // uart0_buf_len = 0;
                // uart0_data_arrived = 0;
                // continue;
            // }
            // if(cm_cmd_engine("CM",uart0_buf,&uart0_buf_len,uart0_cmd_buf,&uart0_cmd_len) == -1)
            // {
                // cm_printf("error\n");
                // uart0_buf_len = 0;
                // osMutexRelease( uart0_cmd_mutex );
            // }
            // else
            // {
                // uart0_buf_len = 0;
                // osSignalSet(OC_Main_TaskHandle, 0x0004);   
            // }
          
            uart0_data_arrived = 0;
        }
        osDelay(20);   
    }
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart0的初始化方法，只是硬件的初始化
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_firmware_uart0_init(void)
{
    cm_uart_cfg_t cfg;
    
    osThreadDef(OC_Uart_Task, atol_firmware_uart0_recv_task, osPriorityNormal, 0, 1024);
    ATOL_Uart0_TaskHandle = osThreadCreate(osThread(OC_Uart_Task), 0);
	
    // 创建互斥量，但是在接收的线程中没有添加互斥量锁线程
    uart0_cmd_mutex = osMutexCreate( osMutex(uart0_cmdMutex) );
	
	
    cfg.id = OPENCPU_UART0;
    cfg.baud = CM_UART_BAUD_115200;
    cfg.databit = CM_UART_DATA_BITS_8;
    cfg.stopbit = CM_UART_STOP_BITS_1;
    cfg.parity = CM_UART_NO_PARITY;
    cfg.cb = atol_oc_uart0_cb;
    cfg.param = "test param";
    cfg.event = CM_UART_EVENT_RX_ARRIVED;
	
    cfg.rx_buf_size = 2*1024;
    cfg.tx_buf_size = 2*1024;
    cm_uart_init(&cfg);
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart0接收到数据的任务，接收到数据就存入到queue中。该任务就一直循环读取queue，有数据就发送
# 参数介绍 
----------------------------------------------------------------------------------------------------*/
void atol_firmware_task_uart0(void) {
	
	while(true) {
		
		unsigned int num;
		num = osMessageWaiting(g_message_uart0_receive_id);
		cm_printf("cm_uart_demo, atol_firmware_task_uart0, queue num is %d \r\n", num);
		
        if(num == 0) {  
			return;
			
        } else {
			// 该queue中存在消息，读取一条数据
			osEvent message_uart0_receive_event;
			message_uart0_receive_event = osMessageGet(g_message_uart0_receive_id, 200);
			// 打印数据和指针
			cm_printf("cm_uart_demo, atol_firmware_task_uart0, queue data point is %d \r\n", 
				message_uart0_receive_event.value.v);
			
			// 拷贝数据
			unsigned char buf[512];
			memcpy(message_uart0_receive_event.value.v,buf,512*sizeof(unsigned char));
			cm_printf("cm_uart_demo, atol_firmware_task_uart0, queue array is %s \r\n", buf);
			
		}
		
		// 增加阻塞时间
        osDelay(50);   
    }
	
}




// int char_to_int(unsigned char *s)
// {
	// int i;  
    // int n = 0;  
    // for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)  
    // {  
        // n = 10 * n + (s[i] - '0');  
    // }  
    // return n;  
// }
// void cm_printf (char *str, ...)
// {

    // static  char s[600]; //This needs to be large enough to store the string TODO Change magic number
    // va_list args;
    // int len;
    // if ((str == NULL) || (strlen(str) == 0))
    // {
        // return;
    // }
    // va_start (args, str);
    // len = vsprintf ((char*)s, str, args);
    // va_end (args);
    // cm_uart_send_no_cache(OPENCPU_UART0,s,len,100);
  
// }


